package com.hsck.order.kds.base.common

import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import coil.request.Disposable

/**
 * 创建者 : ly
 * 时间 : 2017/9/13
 * 公司 : 工博
 * 功能 : 封装BaseFragment
 */
abstract class BaseKtFragment : Fragment() {

    private var isPrepared = false
    private var isResume = true
    //activity跳转后刷新数据
    private var isPause = false
    val disposableList = arrayListOf<Disposable>()

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        isPrepared = true
        view.isClickable = true
        initView()
        getArgumentsData()
    }

    override fun onResume() {
        super.onResume()
        if (isPause){
            isPause = false
            pauseRefreshData()
        }
    }

    override fun onPause() {
        super.onPause()
        isPause = true
    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        if (!hidden) {
            switchRefreshData()
        }
    }

    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
        if (userVisibleHint) {
            if (isPrepared) {
                isResume = true
                vpRefreshData()
            }
        } else {
            isResume = false
        }
    }

    abstract fun initView()

    open fun getArgumentsData() {

    }

    open fun vpRefreshData() {

    }

    open fun switchRefreshData() {

    }

    override fun onDestroy() {
        super.onDestroy()
        if (disposableList.isNotEmpty()) {
            disposableList.forEach {
                it.dispose()
            }
            disposableList.clear()
        }
    }

    //这个方法会被activity加载的所有fragment调用
    //但是因为所有界面已经加载完成 不会有界面相关问题
    open fun pauseRefreshData(){

    }

}